<?php
namespace app\common\model;

use think\facade\Cache;
use think\Model;

class PaymentAccount extends TimeModel
{
    protected $json=['pay_types'];
    protected $jsonAssoc = true;


    static function onAfterUpdate(Model $model){
        cache::delete('pay_'.$model->shou_fu.'_'.$model->pay_type);
    }

    static function onAfterInsert(Model $model){
        cache::delete('pay_'.$model->shou_fu.'_'.$model->pay_type);
    }

    public function getRowById($id)
    {
        $account=$this->find($id);
        if(!$account){
            dd('没有符合条件的支付方式');
        }

        $alias=$account['alias'];

        $category=(new Payment())->where('alias',$alias)->find();
        $_payconfig['rate']=$category['rate'];

        $payconfig=array_merge($account->toArray(),$account['config'],$_payconfig);

        unset($payconfig['config']);
        return $payconfig;
    }

    public static function getItem($value,$field='pay_types',$shouFu=1,$lunxun=0){

        $cacheName='pay_'.$shouFu.'_'.$value;
        if($lunxun==0&&$field=='pay_types'&&Cache::has($cacheName)){
            $data=Cache::get($cacheName);
        }else {
            if($field=='pay_types'){
                $where[] = [
                    $field ,'like',"%".$value."%"
                ];
            }else {
                $where = [
                    $field => $value,
                ];
            }
            $data = (new static())
                ->where('shou_fu', $shouFu)
                ->where('status', 1)
                ->where($where)
                ->order('use_count', 'asc')
                ->find();
            if($data) {
                $_payconfig = $data['config'];
                $data = array_merge($_payconfig, $data->toArray());
                unset($data['config']);
            }
            Cache::set($cacheName,$data);
        }
        return $data;
    }


    public static function getShouPayments(){
        $list=(new Payment())->alias('a')
            ->join('payment_account b','a.alias=b.alias')
            ->where('shou_fu',1)
            ->where('a.status',1)
            ->where('b.status',1)
            ->order('use_count','asc')
            ->field('a.rate,b.*')
            ->select();
        $arr=[];
        if($list) {
            foreach ($list->toArray() as $vv) {
                foreach (json_decode($vv['pay_types'],true) as $v) {
                    $arr[$v][] = $vv;
                }
            }
        }
        return $arr;
    }

    public static function getFuPayments(){
        $list=(new Payment())->alias('a')
            ->join('payment_account b','a.alias=b.alias')
            ->where('shou_fu',2)
            ->where('a.status',1)
            ->where('b.status',1)
            ->order('use_count','asc')
            ->field('a.rate,b.*')
            ->select();
        $arr=[];
        if($list) {
            foreach ($list->toArray() as $vv) {
                foreach (json_decode($vv['pay_types'],true) as $v) {
                    $arr[$v][] = $vv;
                }
            }
        }
        return $arr;
    }

    public function setConfigAttr($value){
        return encrypt(json_encode($value));
    }

    public function getConfigAttr($value){
        return json_decode(decrypt($value),true);
    }
}